package com.breeze.leetcode.middle;

/**
 * @author yangzh
 * @url <a href="https://leetcode.cn/problems/minimum-limit-of-balls-in-a-bag/">1760. 袋子里最少数目的球</a>
 * @date 2025/2/12
 */
public class T1760MinimumLimitOfBallsInBag {

    public static void main(String[] args) {
        int[] nums = {2,4,8,2};
        T1760MinimumLimitOfBallsInBag solution = new T1760MinimumLimitOfBallsInBag();
        int result = solution.minimumSize(nums, 4);
        System.out.printf("result = %d", result);
    }

    /**
     * 二分排查合适的结果，根据结果判断符合最大的操作数
     */
    public int minimumSize(int[] nums, int maxOperations) {
        long left = 1, right = 0;
        for (int num : nums) {
            right = Math.max(right, num);
        }

        while (left < right) {
            long mid = (left + right) / 2;
            long operations = 0;

            for (int num : nums) {
                operations += (num - 1) / mid;
            }

            if (operations > maxOperations) {
                left = mid + 1;
            } else {
                right = mid;
            }
        }

        return (int) left;
    }
}
